package com.kun.alipay.controller;

import com.kun.alipay.common.BaseResponse;
import com.kun.alipay.common.ResultUtils;
import com.kun.alipay.model.Payment;
import com.kun.alipay.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 支付接口
 */
@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    /**
     * 发起支付请求
     *
     * @param payment 支付信息
     * @return 支付页面
     */
    @PostMapping("/pay")
    public BaseResponse<String> pay(@RequestBody Payment payment) {
        String result = paymentService.pay(payment);
        return ResultUtils.success(result);
    }

    /**
     * 支付宝异步通知接口
     *
     * @param request HttpServletRequest
     * @return 处理结果
     */
    @PostMapping("/fallback")
    public BaseResponse<String> fallback(HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        String tradeNo = map.get("out_trade_no")[0];
        return ResultUtils.success(tradeNo);
    }

}
